home *** CD-ROM | disk | FTP | other *** search
- real dataonly[][2];
- real xdata[],ydata[];
- real dataMax, dataMin; ** corrected for roundoff error
- integer maxSize; ** maximum size of data table (see createBlock msg)
- integer max; ** length of data array
- integer x,y;
- real tempx,tempy, xvalue, yvalue;
- double timeArray[];
-
- ** This block calculates an output from an input based on a table.
- ** Copyright © 1989-1994 by Imagine That, Inc.
- ** All Rights Reserved.
- ** Extend Generic Library, Conversion Table block; Alfy Riddle
- ** modified 3/1/92 JSL for V2.0
- ** 8/18/93 JSL pass noValues through
- ** 3/10/94 DJK modified report
- ** 3/10/94 DJK added block label to trace & report
- ** 5/4/94 DJK modified checkdata so that using row 500 would not cause an out of bounds error
- **
- ** The sort fcn of this block came from the:
- ** Nonlinear Function Generator 2/20/89
- ** Authors: Cheryl Blanford and Chuck Oman
- ** MIT Man Vehicle Laboratory
- ** Rm 37-219, Cambridge, MA 02139
-
-
- procedure getValue()
- {
- integer lastX,notDone;
-
- if (noValue(xIn))
- {
- yOut = xIn;
- return;
- }
-
- lastX = 0; ** this is a starting point for the interpolation
- ** it keeps track of the last interpolated point
-
- ** use the input and interpolate to find the y value.
- xvalue = xin;
- if(xvalue<dataMin || xvalue>dataMax) ** avoids roundoff error msg first pt
- {
- if( stop )
- { ** show input
- usererror("Conversion Table input ("+xvalue+") outside valid range in block number "+(MyBlockNumber()));
- abort;
- }
- else
- yOut = 0.0;
- }
- else
- {
- for x=0 to (max-1)
- {
- if (xvalue == data[x][0])
- {
- yout = data[x][1];
- break;
- }
-
- if (xvalue < data[x][0])
- {
- if( interp ) ** interpolate
- {
- yvalue = ((xvalue - data[x-1][0])*(data[x][1] -
- data[x-1][1])/(data[x][0] - data[x-1][0]))
- + data[x-1][1];
- }
- else ** stepped
- {
- yValue = data[x-1][1];
- }
- yOut = yValue;
- break;
- }
- } ** for loop
- }
-
- ** sysGlobal2 is the file reference number for the DEBUG TRACE
- if( sysGlobal2 != 0.0 ) ** 0 is error, check for open file for TRACE
- {
- // template for report: |BLOCK NAME *****************|block number |BLOCK NUMBER*******
- fileWrite(sysGlobal2,"Conversion Table block number "+(MyBlockNumber())+". Current Time:"+currentTime+".","",True);
- if(getBlockLabel(myBlockNumber()) != "")
- fileWrite(sysGlobal2,"Block Label: "+getBlockLabel(myBlockNumber()),"",True);
- fileWrite(sysGlobal2," Input = "+xIn,"",True);
- fileWrite(sysGlobal2," Output = "+yOut,"",True);
- fileWrite(sysGlobal2," ","",True);
- }
- }
-
-
- Procedure validMax()
- {
- integer i;
- i=0;
-
- while( i<maxSize && !noValue(data[i][0]) )
- {
- i++;
- }
- max = i;
-
- if (max <= 1) ** check for at least 2 rows
- {
- usererror("Must have at least two rows of data in Conversion \
- Table block number "+MyBlockNumber());
- abort;
- }
- }
-
-
- Procedure sortFcn()
- {
- validMax();
-
- ** split data into 2 arrays for ease of sorting.
- makearray(dataonly,max);
- makearray(xdata,max);
- makearray(ydata,max);
-
- for (x=0;x<max;x++)
- {
- dataonly[x][0] = data[x][0];
- dataonly[x][1] = data[x][1];
- }
-
- for(x = 0 ;x<max;x++)
- {
- xdata[x] = data[x][0];
- ydata[x] = data[x][1];
- }
-
- for (x=0;x<max;x++)
- for (y=x+1;y<max;y++)
- if(xdata[y] < xdata[x])
- {
- tempx = xdata[y];
- tempy = ydata[y];
- xdata[y] = xdata[x];
- ydata[y] = ydata[x];
- xdata[x] = tempx;
- ydata[x] = tempy;
- }
-
-
- **restore table, now properly sorted.
- for (x = 0;x<max;x++ )
- {
- data[x][0] = xdata[x];
- data[x][1] = ydata[x];
- }
- }
-
-
- on choosetoplot
- {
- integer k, numPlot;
- validMax();
-
- ** support stepped plots
- if( interp )
- numPlot = max;
- else if( stepped )
- numPlot = 2 * max - 1;
-
- makearray(xdata,numPlot);
- makearray(ydata,numPlot);
-
- for (k=0;k<max;k++)
- {
- if( interp )
- {
- xdata[k] = data[k][0];
- ydata[k] = data[k][1];
- }
- else ** stepped
- {
- xdata[2*k] = data[k][0];
- ydata[2*k] = data[k][1];
-
- if( k < max-1 ) ** odd number of points (skip last)
- {
- xdata[2*k+1] = data[k+1][0]; ** x coordinates
- ydata[2*k+1] = data[k][1];
- }
- }
- }
-
- ** install the axes
- installAxis(0, "Conversion Table Curve",
- "X In", FALSE, 0,20,
- "Y Out", FALSE, 0,20, "", 0, 0, 0,
- blackpattern, blackcolor, 100);
- installArray(0, 0, "X In", xdata, 0, 20,
- 0, 0, blackPattern, cyanColor);
- installArray(0, 1, "Y Out", ydata, 0, 20,
- 0, 0, dkgrayPattern, redColor);
-
- makeScatter(0, 0);
- ** plot the data
-
- for (k=0;k<numPlot;k++)
- plotNewScatter(0,0,k,xdata[k],ydata[k]);
-
- autoscalex(0);
- autoscaley(0);
- showPlot(0,"Conversion Table Curve");
- }
-
-
- ** This message occurs for each step in the simulation.
- on simulate
- {
- getValue();
- }
-
-
- on endsim
- {
- integer i;
-
- ** sysGlobal1 is the file reference number for the REPORT
- if( sysGlobal1 != 0.0 ) ** 0 is error, check for open file for REPORT
- {
- // template for report: |BLOCK NAME *****************|block number |BLOCK NUMBER*******
- fileWrite(sysGlobal1,"Conversion Table block number "+MyBlockNumber(),"",True);
- if(getBlockLabel(myBlockNumber()) != "")
- fileWrite(sysGlobal1,"Block Label: "+getBlockLabel(myBlockNumber()),"",True);
- if( interp )
- fileWrite(sysGlobal1," Interpolated","",True);
- else
- fileWrite(sysGlobal1," Stepped","",True);
- if( stop )
- fileWrite(sysGlobal1," Stops when out of range","",True);
- else
- fileWrite(sysGlobal1," Sets output to zero when out of range","",True);
- fileWrite(sysGlobal1," Table x y","",TRUE);
-
- validMax();
-
- for( i=0;i<max;i++ ) ** picks up old itemNum
- fileWrite(sysGlobal1," "+RealToStr(data[i][0],8)+" "+RealToStr(data[i][1],8),"",TRUE);
- if( comments != "" )
- fileWrite(sysGlobal1," Comments = "+comments,"",True);
- fileWrite(sysGlobal1," ","",True);
- }
- }
-
-
- on createBlock
- {
- maxSize = 500;
- interp = 1;
- stepped = 0;
- stop = 1;
- zero = 0;
- }
-
-
- on checkdata
- {
- sysGlobal1 = 0.0; ** prevent false reports
- sysGlobal2 = 0.0; ** prevent false debugs
-
- validMax(); ** check for at least 2 rows
- }
-
-
- ** Initialize any simulation variables.
- on initsim
- {
- sortFcn(); ** sort the table
-
- if( data[max-1][0] > 0 ) ** for roundoff error
- dataMax = data[max-1][0]*1.000000001;
- else
- dataMax = data[max-1][0]*0.999999999;
-
- if( data[0][0] > 0 )
- dataMin = data[0][0]*0.999999999;
- else
- dataMin = data[0][0]*1.000000001;
-
- if( getPassedArray(sysGlobal0, timeArray) )
- getSimulateMsgs(FALSE);
- }
-
-
- on sort
- {
- sortFcn(); ** then sort the table
- }